from file.models import KnowledgeBase
from rest_framework import serializers



class KnowledgeBaseSerializer(serializers.ModelSerializer):
    create_date = serializers.DateTimeField(format=None)  # 返回 datetime 对象
    class Meta:
        model = KnowledgeBase
        fields = '__all__'

def get_all_know_bases():
    knowbases = KnowledgeBase.objects.all()
    result = []
    if len(knowbases) == 0:
        return result
    serializer = KnowledgeBaseSerializer(knowbases, many=True)
    return serializer.data

def get_knowbase_by_name(knowbase_name):
    knowbases = KnowledgeBase.objects.filter(know_base_name=knowbase_name)
    if len(knowbases) == 0:
        return None
    serializer = KnowledgeBaseSerializer(knowbases, many=True)
    return serializer.data

def get_knowbase_by_id(knowbase_id):
    knowbases = KnowledgeBase.objects.filter(know_base_id=knowbase_id)
    if len(knowbases) == 0:
        return None
    serializer = KnowledgeBaseSerializer(knowbases, many=True)
    return serializer.data

def delete_knowbase_by_id(knowbase_id):
    delete_count,delete_details = KnowledgeBase.objects.filter(know_base_id=knowbase_id).delete()
    return delete_count

